<template>
  <div>
    <el-table
      :data="list"
      :header-cell-style="{
        color: '#333333',
        'background-color': '#E7E7E7',
        'text-align': 'center',
        'font-size':'16px',
        'font-weight':'400'
      }"
      :highlight-current-row="true"
      :max-height="1500"
      :size="size"
      v-loading="loading"
      align="align"
      header-row-class-name="tableHead"
      class="table"
    >
      <el-table-column width="300" align="center" prop="promotionCode" label="活动ID">
      </el-table-column>
      <el-table-column width="240" align="center" prop="promName" label="活动名称">
      </el-table-column>
      <el-table-column width="360" align="center" label="活动时间">
        <template slot-scope="scope">
          <span>{{ scope.row.startTime }} 至 {{ scope.row.endTime }}</span>
        </template>
      </el-table-column>
      <el-table-column width="100" align="center" prop="address" label="活动状态">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 9">草稿状态</span>
          <span v-if="scope.row.status == 1">待提交</span>
          <span v-else-if="scope.row.status == 2">待审核</span>
          <span v-else-if="scope.row.status == 3">未开始</span>
          <span v-else-if="scope.row.status == 4">
            审核不通过
            <el-tooltip
              effect="dark"
              :content="scope.row.auditRemark"
              placement="right"
            >
<!--              <i class="el-icon-question"></i>-->
            </el-tooltip>
          </span>
          <span v-else-if="scope.row.status == 5">进行中</span>
          <span v-else-if="scope.row.status == 6">已过期</span>
          <span v-else-if="scope.row.status == 7">已关闭</span>
          <span v-else-if="scope.row.status == 8">已结束</span>
        </template>
      </el-table-column>
      <el-table-column width="100" align="center" label="活动渠道" >
        <template slot-scope="scope">
          <span>{{ scope.row.channelCodes }}</span>
        </template>
      </el-table-column>
      <el-table-column width="240" align="center" prop="creatorName" label="创建人">
      </el-table-column>
      <el-table-column width="240" align="center" prop="createTime" label="创建时间">
      </el-table-column>
  
      <!--      <el-table-column class-name="status-col" align="center" label="实际参与人数" width="170">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ scope.row.numOfparticipants }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column class-name="status-col" align="center" label="实际投入成本" width="170">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ scope.row.inputCost }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column class-name="status-col" align="center" label="实际营销额" width="170">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ scope.row.marketingAmount }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column class-name="status-col" align="center" label="实际核销数" width="170">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ scope.row.writeOff}}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column
        class-name="status-col"
        label="操作"  
        width="390"
        align="left"
        fixed="right"
        class="btns"
      >
        <template slot-scope="scope">
          <span v-if="[3, 5].includes(scope.row.status) && scope.row.editFlag" >
            <el-button
              type="text"
              class="textButton"
              @click="seeNewTab(scope.row)"
              >查看</el-button
            >
            <kt-button
              type="text"
              class="textButton"
              @click="copyBtnShow(scope.row.id)"
              perms="market:increase:copy"
              label="复制"
            />
            <kt-button
              type="text"
              class="textButton"
              @click="closeActivity(scope.row)"
              perms="market:increase:close"
              label="关闭"
            />
            <!--            <el-button type="text" class="textButton" @click="viewMarketingReport(scope.row)">查看营销报告</el-button>-->
          </span>
          <!-- <span v-else-if="[0].includes(scope.row.status)">
            <kt-button
              type="text"
              class="textButton"
              @click="updateBtnShow(scope.row)"
              perms="market:increase:edit"
              label="编辑"
            />
            <kt-button
              type="text"
              class="textButton"
              @click="deleteActivity(scope.row.id)"
              perms="market:increase:delete"
              label="删除"
            />
            </span> -->
          <span v-else-if="[9].includes(scope.row.status)  && scope.row.editFlag" >
             <kt-button type="text" class="textButton" @click="updateBtnShow(scope.row)" perms="market:increase:edit" label="编辑"/>
             <kt-button type="text" class="textButton" @click="deleteActivity(scope.row.id)" perms="market:increase:delete" label="删除"/>
          </span>
          <span v-else-if="[2, 6, 7,8].includes(scope.row.status)  && scope.row.editFlag" >
            <el-button
              type="text"
              class="textButton"
              @click="seeNewTab(scope.row)"
              >查看</el-button
            >
            <kt-button
              type="text"
              class="textButton"
              @click="copyBtnShow(scope.row.id)"
              perms="market:increase:copy"
              label="复制"
            />
            <!--            <el-button type="text" class="textButton" @click="viewMarketingReport(scope.row)">查看营销报告</el-button>-->
          </span>
          <span v-else-if="[4].includes(scope.row.status) && scope.row.editFlag"  >
            <el-button
              type="text"
              class="textButton"
              @click="seeNewTab(scope.row)"
              >查看</el-button
            >
            <kt-button
              type="text"
              class="textButton"
              @click="copyBtnShow(scope.row.id)"
              perms="market:increase:copy"
              label="复制"
            />
            <kt-button
              type="text"
              class="textButton"
              @click="updateBtnShow(scope.row)"
              perms="market:increase:edit"
              label="编辑"
            />
          </span>
          <span v-else-if="[1].includes(scope.row.status) && scope.row.editFlag"  >
            <el-button
              type="text"
              class="textButton"
              @click="seeNewTab(scope.row)"
              >查看</el-button
            >
            <kt-button
              type="text"
              class="textButton"
              @click="copyBtnShow(scope.row.id)"
              perms="market:increase:copy"
              label="复制"
            />
            <kt-button
              type="text"
              class="textButton"
              @click="updateBtnShow(scope.row)"
              perms="market:increase:edit"
              label="编辑"
            />
            <kt-button
              type="text"
              class="textButton"
              @click="deleteActivity(scope.row.id)"
              perms="market:increase:delete"
              label="删除"
            />
          </span>
          <span v-else-if="!scope.row.editFlag">
            <el-button type="text" class="textButton" @click="seeNewTab(scope.row)">查看</el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>
    <!--分页栏-->
    <div class="toolbar" >
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="refreshPageRequest"
        :current-page="listQuery.pageNo"
        :page-size="listQuery.pageSize"
        :total="totalSize"
        style="float:right;"
        :page-sizes="[10, 20, 30, 40]"
        @size-change="handleSizeChange"
        class="pagination"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import KtButton from "@/views/Core/KtButton";
import marketRequest from "@/market/request/marketRequest";
import {
  promTypeConverter,
  frontPromTypeConverter,
  channelCodeConverter
} from "@/market/dict/marketDict";

export default {
  components: {
    KtButton
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "info",
        deleted: "danger"
      };
      return statusMap[status];
    }
  },
  props: {
    type: {
      type: String,
      default: "0"
    },
    parentFilters: {
      type: Object
    }
  },
  data() {
    return {
      size: "small",
      list: [],
      tableList: {
        status0: [
          {
            activityId: 1912050009905594,
            title: "323 - 复制",
            promType: 1,
            frontPromotionType: "满额赠",
            startTime: "2019-12-05 20:09:02",
            endTime: "2019-12-18 20:09:04",
            status: "已过期",
            remark: null,
            createUsername: "superadmin",
            createUserid: 19090215374897,
            createTime: "2019-12-25 10:54:49",
            isDeleted: 0,
            companyId: 276,
            createMerchantId: null,
            createMerchantName: null,
            activityType: "自建",
            merchantType: 1,
            promPlatform: 0,
            payType: null,
            canCreateActivity: 1,
            ruleDesc: null,
            scopeType: null,
            channelCodes: "BBC",
            hasWriteRoll: true,
            type: "2",
            numOfparticipants: 0,
            inputCost: 0,
            marketingAmount: 0,
            writeOff: 0
          },
          {
            activityId: 1912240002354936,
            title: "如果是 - 复制 - 复制",
            promType: 1,
            frontPromotionType: "折扣",
            startTime: "2019-12-13 16:23:56",
            endTime: "2019-12-28 16:23:57",
            status: "进行中",
            remark: null,
            createUsername: "superadmin",
            createUserid: 19090215374897,
            createTime: "2019-12-25 10:54:49",
            isDeleted: 0,
            companyId: 276,
            createMerchantId: null,
            createMerchantName: null,
            activityType: "自建",
            merchantType: 1,
            promPlatform: 11,
            payType: null,
            canCreateActivity: 1,
            ruleDesc: "答复",
            scopeType: null,
            channelCodes: "BBC",
            hasWriteRoll: true,
            type: "1",
            numOfparticipants: 0,
            inputCost: 0,
            marketingAmount: 0,
            writeOff: 0
          },
          {
            activityId: 1912240002478469,
            title: "asdasd",
            promType: 1,
            frontPromotionType: "特价",
            startTime: "2019-12-03 14:49:25",
            endTime: "2019-12-19 14:49:26",
            status: "	已过期",
            remark: null,
            createUsername: "merchant",
            createUserid: 1909160000172983,
            createTime: "2019-12-10 10:52:13",
            isDeleted: 0,
            companyId: 276,
            createMerchantId: null,
            createMerchantName: null,
            activityType: "自建",
            merchantType: 1,
            promPlatform: 11,
            payType: null,
            canCreateActivity: 1,
            ruleDesc: "asdasdas",
            scopeType: null,
            channelCodes: "BBC",
            hasWriteRoll: true,
            type: "2",
            numOfparticipants: 0,
            inputCost: 0,
            marketingAmount: 0,
            writeOff: 0
          },
          {
            activityId: 1912100005294293,
            title: "非会员001 - 复制",
            promType: 1,
            frontPromotionType: "特价",
            startTime: "2019-11-22 17:02:13",
            endTime: "2019-11-23 17:02:15",
            status: "	已过期",
            remark: null,
            createUsername: "superadmin",
            createUserid: 19090215374897,
            createTime: "2019-12-24 14:50:14",
            isDeleted: 0,
            companyId: 276,
            createMerchantId: null,
            createMerchantName: null,
            activityType: "自建",
            merchantType: 1,
            promPlatform: 0,
            payType: null,
            canCreateActivity: 1,
            ruleDesc: null,
            scopeType: null,
            channelCodes: "BBC",
            hasWriteRoll: true,
            type: "2",
            numOfparticipants: 0,
            inputCost: 0,
            marketingAmount: 0,
            writeOff: 0
          },
          {
            activityId: 1911220011038757,
            title: "非会员001",
            promType: 1,
            frontPromotionType: "直降",
            startTime: "2019-11-22 17:02:13",
            endTime: "2019-11-23 17:02:15",
            status: "	已过期",
            remark: null,
            createUsername: "superadmin",
            createUserid: 19090215374897,
            createTime: "2019-11-22 17:02:40",
            isDeleted: 0,
            companyId: 276,
            createMerchantId: null,
            createMerchantName: null,
            activityType: "自建",
            merchantType: 1,
            promPlatform: 0,
            payType: null,
            canCreateActivity: 1,
            ruleDesc: null,
            scopeType: null,
            channelCodes: "BBC",
            hasWriteRoll: true,
            type: "2",
            numOfparticipants: 0,
            inputCost: 0,
            marketingAmount: 0,
            writeOff: 0
          },
          {
            activityId: 1912050007028843,
            title: "cs001",
            promType: 1,
            frontPromotionType: "特价",
            startTime: "2019-12-05 14:20:58",
            endTime: "2019-12-12 14:20:59",
            status: "已过期",
            remark: null,
            createUsername: "superadmin",
            createUserid: 19090215374897,
            createTime: "2019-12-05 14:22:22",
            isDeleted: 0,
            companyId: 276,
            createMerchantId: null,
            createMerchantName: null,
            activityType: "自建",
            merchantType: 1,
            promPlatform: 0,
            payType: null,
            canCreateActivity: 1,
            ruleDesc: null,
            scopeType: null,
            channelCodes: "BBC",
            hasWriteRoll: true,
            type: "5",
            numOfparticipants: 0,
            inputCost: 0,
            marketingAmount: 0,
            writeOff: 0
          },
          {
            activityId: 1912050006571923,
            title: "cs",
            promType: 1,
            frontPromotionType: "满额赠",
            startTime: "2019-12-05 13:20:13",
            endTime: "2019-12-06 13:20:15",
            status: "待提交",
            remark: null,
            createUsername: "superadmin",
            createUserid: 19090215374897,
            createTime: "2019-12-05 13:21:38",
            isDeleted: 0,
            companyId: 276,
            createMerchantId: null,
            createMerchantName: null,
            activityType: "自建",
            merchantType: 1,
            promPlatform: 0,
            payType: null,
            canCreateActivity: 1,
            ruleDesc: "321",
            scopeType: null,
            channelCodes: "BBC",
            hasWriteRoll: true,
            type: "5",
            numOfparticipants: 0,
            inputCost: 0,
            marketingAmount: 0,
            writeOff: 0
          }
        ],
        status1: [
          {
            activityId: 1912250005250786,
            title: "自测折上折",
            promType: 1,
            frontPromotionType: "特价",
            startTime: "2019-12-24 10:53:49",
            endTime: "2020-01-04 10:53:52",
            status: "进行中",
            remark: null,
            createUsername: "superadmin",
            createUserid: 19090215374897,
            createTime: "2019-12-25 10:54:49",
            isDeleted: 0,
            companyId: 276,
            createMerchantId: null,
            createMerchantName: null,
            activityType: "自建",
            merchantType: 1,
            promPlatform: 0,
            payType: null,
            canCreateActivity: 1,
            ruleDesc: null,
            scopeType: null,
            channelCodes: "BBC",
            hasWriteRoll: true,
            type: "1",
            numOfparticipants: 0,
            inputCost: 0,
            marketingAmount: 0,
            writeOff: 0
          },
          {
            activityId: 1912240002354936,
            title: "如果是 - 复制 - 复制",
            promType: 1,
            frontPromotionType: "折扣",
            startTime: "2019-12-13 16:23:56",
            endTime: "2019-12-28 16:23:57",
            status: "进行中",
            remark: null,
            createUsername: "superadmin",
            createUserid: 19090215374897,
            createTime: "2019-12-25 10:54:49",
            isDeleted: 0,
            companyId: 276,
            createMerchantId: null,
            createMerchantName: null,
            activityType: "自建",
            merchantType: 1,
            promPlatform: 11,
            payType: null,
            canCreateActivity: 1,
            ruleDesc: "答复",
            scopeType: null,
            channelCodes: "BBC",
            hasWriteRoll: true,
            type: "1",
            numOfparticipants: 0,
            inputCost: 0,
            marketingAmount: 0,
            writeOff: 0
          }
        ],
        status2: [
          {
            activityId: 1912240002478469,
            title: "asdasd",
            promType: 1,
            frontPromotionType: "特价",
            startTime: "2019-12-03 14:49:25",
            endTime: "2019-12-19 14:49:26",
            status: "	已过期",
            remark: null,
            createUsername: "merchant",
            createUserid: 1909160000172983,
            createTime: "2019-12-10 10:52:13",
            isDeleted: 0,
            companyId: 276,
            createMerchantId: null,
            createMerchantName: null,
            activityType: "自建",
            merchantType: 1,
            promPlatform: 11,
            payType: null,
            canCreateActivity: 1,
            ruleDesc: "asdasdas",
            scopeType: null,
            channelCodes: "BBC",
            hasWriteRoll: true,
            type: "2",
            numOfparticipants: 0,
            inputCost: 0,
            marketingAmount: 0,
            writeOff: 0
          },
          {
            activityId: 1912100005294293,
            title: "非会员001 - 复制",
            promType: 1,
            frontPromotionType: "特价",
            startTime: "2019-11-22 17:02:13",
            endTime: "2019-11-23 17:02:15",
            status: "	已过期",
            remark: null,
            createUsername: "superadmin",
            createUserid: 19090215374897,
            createTime: "2019-12-24 14:50:14",
            isDeleted: 0,
            companyId: 276,
            createMerchantId: null,
            createMerchantName: null,
            activityType: "自建",
            merchantType: 1,
            promPlatform: 0,
            payType: null,
            canCreateActivity: 1,
            ruleDesc: null,
            scopeType: null,
            channelCodes: "BBC",
            hasWriteRoll: true,
            type: "2",
            numOfparticipants: 0,
            inputCost: 0,
            marketingAmount: 0,
            writeOff: 0
          },
          {
            activityId: 1911220011038757,
            title: "非会员001",
            promType: 1,
            frontPromotionType: "直降",
            startTime: "2019-11-22 17:02:13",
            endTime: "2019-11-23 17:02:15",
            status: "	已过期",
            remark: null,
            createUsername: "superadmin",
            createUserid: 19090215374897,
            createTime: "2019-11-22 17:02:40",
            isDeleted: 0,
            companyId: 276,
            createMerchantId: null,
            createMerchantName: null,
            activityType: "自建",
            merchantType: 1,
            promPlatform: 0,
            payType: null,
            canCreateActivity: 1,
            ruleDesc: null,
            scopeType: null,
            channelCodes: "BBC",
            hasWriteRoll: true,
            type: "2",
            numOfparticipants: 0,
            inputCost: 0,
            marketingAmount: 0,
            writeOff: 0
          }
        ],
        status5: [
          {
            activityId: 1912050007028843,
            title: "cs001",
            promType: 1,
            frontPromotionType: "特价",
            startTime: "2019-12-05 14:20:58",
            endTime: "2019-12-12 14:20:59",
            status: "待提交",
            remark: null,
            createUsername: "superadmin",
            createUserid: 19090215374897,
            createTime: "2019-12-05 14:22:22",
            isDeleted: 0,
            companyId: 276,
            createMerchantId: null,
            createMerchantName: null,
            activityType: "自建",
            merchantType: 1,
            promPlatform: 0,
            payType: null,
            canCreateActivity: 1,
            ruleDesc: null,
            scopeType: null,
            channelCodes: "BBC",
            hasWriteRoll: true,
            type: "5",
            numOfparticipants: 0,
            inputCost: 0,
            marketingAmount: 0,
            writeOff: 0
          },
          {
            activityId: 1912050006571923,
            title: "cs",
            promType: 1,
            frontPromotionType: "特价",
            startTime: "2019-12-05 13:20:13",
            endTime: "2019-12-06 13:20:15",
            status: "待提交",
            remark: null,
            createUsername: "superadmin",
            createUserid: 19090215374897,
            createTime: "2019-12-05 13:21:38",
            isDeleted: 0,
            companyId: 276,
            createMerchantId: null,
            createMerchantName: null,
            activityType: "自建",
            merchantType: 1,
            promPlatform: 0,
            payType: null,
            canCreateActivity: 1,
            ruleDesc: "321",
            scopeType: null,
            channelCodes: "BBC",
            hasWriteRoll: true,
            type: "5",
            numOfparticipants: 0,
            inputCost: 0,
            marketingAmount: 0,
            writeOff: 0
          }
        ]
      },

      listQuery: {
        pageNo: 1,
        pageSize: 10,
        status: this.type,
        sort: "+id"
      },
      loading: false,
      totalSize: 0
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      this.$emit("create"); // for test
      let _filters = JSON.parse(JSON.stringify(this.parentFilters));

      _filters["pageNo"] = this.listQuery.pageNo;
      _filters["pageSize"] = this.listQuery.pageSize;
      _filters["status"] = this.listQuery.status;
      if (_filters.status == "-1") {
        _filters.status = "";
      }
      let this_ = this;
      this.utils.request.promotionList(_filters, function(res) {
        if (res.code == "0000") {
          this_.list = res.data.list;
          this_.totalSize = res.data.totalCount;
        } else {
          //请求失败，展示mock数据
          var key = "status" + this_.listQuery.status;
          //this_.list = this_.tableList[key];
        }
        this_.loading = false;
      });

      // })
    },
    //查看
    seeNewTab: function(val) {
      //this.$router.push("/QueryPromotion");
      this.$router.push({ path: "/IncreasePromotionDetail/" + val.id + "/0"});
      //this.$router.push(`/IncreasePromotionDetail?id=${val.id}&auth=0`);
    },
    //复制
    copyBtnShow: function(id) {
      let that = this;
      this.$confirm("确定要复制该活动？", "提示", {
        type: "warning",
        closeOnClickModal: false,
      })
        .then(() => {
          marketRequest.promotionDeepCopy(
            { id: id },
            res => {
              that.$message({ message: "复制成功", type: "success" });
              that.getList();
            }
          );
        })
        .catch(() => {});
      // const loading = this.$loading({
      //   lock: true,
      //   text: "Loading",
      //   spinner: "el-icon-loading",
      //   background: "rgba(0, 0, 0, 0.7)"
      // });
      // let this_ = this;
      // let param = { id: id };
      // marketRequest.promotionDeepCopy(param, function(res) {
      //   if (res.code == "0000") {
      //     this_.getList();
      //     loading.close();
      //   }
      // });
    },
    //关闭
    closeActivity: function(row) {
      let that = this;
      let msg = '该活动即将进行，确定要关闭它？关闭后不可再次开启。'
      if(row.status == 5){
        msg = '该活动正在进行中，确定要关闭它？关闭后不可再次开启。'
      }
      this.$confirm(
        msg,
        "提示",
        {
          type: "warning",
          closeOnClickModal: false,
        }
      )
        .then(() => {
          this.utils.request.promotionUpdateStatus(
            { id: row.id, status: 7 },
            res => {
              that.$message({ message: "关闭成功", type: "success" });
              that.getList();
            }
          );
        })
        .catch(() => {});
    },
    //关闭或者删除
    updatePromotionStatus: function(id, status) {
      let this_ = this;
      let params = { id: id, status: status };
      this.utils.request.promotionUpdateStatus(params, function(res) {
        if (res.code == "0000") {
          this_.getList();
        } else {
          alert("更新失败");
        }
      });
    },
    //删除
    deleteActivity: function(id) {
      this.$confirm("确定要执行删除操作么？删除后不可恢复!", "提示", {
        type: "warning",
        closeOnClickModal: false,
      })
        .then(() => {
          let this_ = this;
          let params = { id: id };
          this.utils.request.promotionDelete(params, function(res) {
            if (res.code == "0000") {
              this_.getList();
            } else {
              alert("更新失败");
            }
          });
        })
        .catch(() => {});
    },
    updateBtnShow: function(val) {
      this.$router.push(`/IncreasePromotionCreate?id=${val.id}&auth=1&active=1`);
    },
    //追加商品
    appendMerchantProduct: function(val) {
      //this.$router.push("/AdditionProduct");
      this.$router.push(
        `/IncreasePromotionCreate?id=${val.id}&auth=0&active=3`
      );
    },
    //查看营销报告
    viewMarketingReport: function() {
      this.$router.push("/MarketingReport");
    },
    // 换页刷新-pageNo
    refreshPageRequest: function(pageNum) {
      this.listQuery.pageNo = pageNum;
      this.getList();
    },
    // 换页刷新-pageSize
    handleSizeChange: function(pageSize) {
      this.listQuery.pageSize = pageSize;
      this.listQuery.pageNo = 1;
      this.getList();
    },
    genFrontPromType: function(type) {
      return frontPromTypeConverter(type);
    },
    genChannelCode: function(code) {
      return channelCodeConverter(code);
    },
    //查看分析
    viewAnalysi() {
      this.$router.push("/SecKillPromotionDetail");
      //  
    },
    //二次营销
    SecondSell() {
      //  
      this.$emit("SecondSell");
    },
  }
};
</script>
<style scoped lang="scss">
.textButton {
  margin-left: 1px;
}
.type {
  display: flex;
  align-items: center;
}
.type p {
  text-align: right;
  margin-right: 20px;
  width: 120px;
}
/deep/ button {
  margin-bottom: 10px;
  margin-left: 10px;
}
.tableHead {
  /deep/ th {
    .cell {
      font-size: 16px ;
    }
  }
}

// .tableRowStyle{
//   color: #666666;
// }


</style>
